// 卡牌数据库
class CardDatabase {
    constructor() {
        this.cards = this.initializeCards();
    }

    // 初始化所有卡牌（重构：按角色与通用分别划分普通/罕见/稀有）
    initializeCards() {
        return {
            characters: {
                xu: {
                    common: [
                // 基础卡牌（普通）
                        new Card('重拳', 1, 'attack', 8, '造成8点伤害', [], false, 'common'),
                        new Card('愤怒', 1, 'skill', 0, '获得2点力量', [], false, 'common'),
                        new Card('坚韧', 1, 'skill', 0, '获得3点护甲和1点力量', [], false, 'common'),
                        new Card('战吼', 1, 'skill', 0, '获得2点力量，抽1张牌', [], false, 'common'),
                        new Card('不屈', 1, 'defend', 0, '获得5点护甲，如果生命值低于50%，获得2点力量', [], false, 'common'),
                        new Card('火焰打击', 2, 'attack', 10, '造成10点法术伤害，如果力量≥3，额外造成5点伤害', [], true, 'common')
                    ],
                    uncommon: [
                        new Card('重击', 2, 'attack', 12, '造成12点伤害', [], false, 'uncommon'),
                        new Card('蛮力', 2, 'attack', 15, '造成15点伤害，获得1点力量', [], false, 'uncommon'),
                        new Card('元素重击', 3, 'attack', 15, '造成15点法术伤害，获得1点力量', [], true, 'uncommon')
                    ],
                    rare: [
                        new Card('粉碎', 3, 'attack', 20, '造成20点伤害', [], false, 'rare'),
                        new Card('终结', 4, 'attack', 30, '造成30点伤害，消耗所有能量', [], false, 'rare')
                    ]
                },
                ling: {
                    common: [
                        new Card('快速攻击', 0, 'attack', 4, '造成4点伤害', [], false, 'common'),
                        new Card('闪避', 1, 'defend', 0, '获得3点护甲，抽1张牌', [], false, 'common'),
                        new Card('连击', 1, 'attack', 6, '造成6点伤害，抽1张牌', [], false, 'common'),
                        new Card('敏捷', 1, 'skill', 0, '获得2点敏捷', [], false, 'common'),
                        new Card('风之舞', 1, 'defend', 0, '获得4点护甲，获得1点敏捷', [], false, 'common'),
                        new Card('冰霜射击', 1, 'attack', 6, '造成6点法术伤害，抽1张牌', [], true, 'common')
                    ],
                    uncommon: [
                        new Card('疾风', 1, 'attack', 8, '造成8点伤害，如果手牌少于3张，抽1张牌', [], false, 'uncommon'),
                        new Card('影分身', 2, 'skill', 0, '获得3点敏捷，抽2张牌', [], false, 'uncommon'),
                        new Card('连环踢', 2, 'attack', 12, '造成12点伤害，抽1张牌', [], false, 'uncommon'),
                        new Card('风刃', 2, 'attack', 9, '造成9点法术伤害，如果敏捷≥3，额外抽1张牌', [], true, 'uncommon'),
                        new Card('闪电链', 2, 'attack', 11, '造成11点法术伤害，获得1点敏捷', [], true, 'uncommon')
                    ],
                    rare: [
                        new Card('瞬移', 1, 'skill', 0, '获得2点敏捷，本回合免疫伤害', [], false, 'rare'),
                        new Card('千手观音', 3, 'attack', 20, '造成20点伤害，抽3张牌', [], false, 'rare')
                    ]
                },
                che: {
                    common: [
                        new Card('基础攻击', 1, 'attack', 5, '造成5点伤害', [], false, 'common'),
                        new Card('基础防御', 1, 'defend', 0, '获得4点护甲', [], false, 'common'),
                        new Card('学习', 1, 'skill', 0, '获得1点智力，抽1张牌', [], false, 'common'),
                        new Card('专注', 1, 'skill', 0, '获得2点智力，抽1张牌', [], false, 'common'),
                        new Card('法术冲击', 1, 'attack', 6, '造成6点法术伤害（受智力加成）', [], true, 'common'),
                        new Card('魔法护盾', 1, 'defend', 0, '获得3点护甲，获得1点智力', [], false, 'common')
                    ],
                    uncommon: [
                        new Card('成长', 2, 'skill', 0, '获得1点力量、敏捷和智力', [], false, 'uncommon'),
                        new Card('创新', 2, 'skill', 0, '获得2点智力，抽2张牌', [], false, 'uncommon'),
                        new Card('智慧之箭', 1, 'attack', 7, '造成7点法术伤害，获得1点智力', [], true, 'uncommon'),
                        new Card('奥术飞弹', 2, 'attack', 12, '造成12点法术伤害，抽1张牌', [], true, 'uncommon'),
                        new Card('智慧风暴', 2, 'attack', 10, '造成10点法术伤害，获得2点智力', [], true, 'uncommon')
                    ],
                    rare: [
                        new Card('适应', 1, 'skill', 0, '根据当前状态获得属性', [], false, 'rare'),
                        new Card('突破', 3, 'skill', 0, '获得3点智力，抽3张牌', [], false, 'rare'),
                        new Card('觉醒', 4, 'skill', 0, '获得5点所有属性', [], false, 'rare'),
                        new Card('元素爆发', 3, 'attack', 18, '造成18点法术伤害', [], true, 'rare'),
                        new Card('知识爆炸', 4, 'attack', 25, '造成25点法术伤害，抽2张牌', [], true, 'rare')
                    ]
                }
            },
            common: {
                common: [
                    new Card('攻击', 1, 'attack', 6, '造成6点伤害', [], false, 'common'),
                    new Card('防御', 1, 'defend', 0, '获得5点护甲', [], false, 'common'),
                    new Card('治疗', 1, 'skill', 0, '恢复8点生命值', [], false, 'common'),
                    new Card('抽牌', 1, 'skill', 0, '抽2张牌', [], false, 'common'),
                    new Card('强化', 1, 'skill', 0, '获得1点力量', [], false, 'common'),
                    new Card('敏捷', 1, 'skill', 0, '获得1点敏捷', [], false, 'common'),
                    new Card('智慧', 1, 'skill', 0, '获得1点智力', [], false, 'common'),
                    new Card('重击', 2, 'attack', 10, '造成10点伤害', [], false, 'common'),
                    new Card('护盾', 2, 'defend', 0, '获得8点护甲', [], false, 'common'),
                    new Card('恢复', 2, 'skill', 0, '恢复15点生命值', [], false, 'common'),
                    new Card('魔法弹', 1, 'attack', 5, '造成5点法术伤害', [], true, 'common'),
                    new Card('火球术', 2, 'attack', 8, '造成8点法术伤害', [], true, 'common'),
                    new Card('奥术智慧', 1, 'skill', 0, '获得1点智力，抽1张牌', [], false, 'common'),
                    // 新增联动卡（普通）
                    new Card('引导', 0, 'skill', 0, '本回合你的下一张法术牌触发两次', [], false, 'common'),
                    new Card('连携打击', 1, 'attack', 6, '造成6点伤害；若本回合已使用≥2张攻击牌，额外+6并抽1张', [], false, 'common')
                ],
                uncommon: [
                    new Card('暴击', 2, 'attack', 15, '造成15点伤害，有50%概率造成双倍伤害', [], false, 'uncommon'),
                    new Card('完美防御', 2, 'defend', 0, '获得10点护甲，免疫下次伤害', [], false, 'uncommon'),
                    new Card('智慧之光', 2, 'skill', 0, '获得3点智力，抽3张牌', [], false, 'uncommon'),
                    new Card('力量爆发', 3, 'skill', 0, '获得5点力量', [], false, 'uncommon'),
                    new Card('敏捷提升', 3, 'skill', 0, '获得5点敏捷', [], false, 'uncommon'),
                    new Card('生命恢复', 2, 'skill', 0, '恢复25点生命值', [], false, 'uncommon'),
                    new Card('能量爆发', 1, 'skill', 0, '获得2点能量', [], false, 'uncommon'),
                    // 新增联动卡（罕见）
                    new Card('破绽识别', 1, 'skill', 0, '使目标获得2层易伤；若其有虚弱或中毒，额外+1层', [], false, 'uncommon'),
                    new Card('节奏掌控', 1, 'skill', 0, '本回合每打出第3张牌：+1护甲并抽1张（最多2次）', [], false, 'uncommon')
                ],
                rare: [
                    new Card('终极攻击', 4, 'attack', 25, '造成25点伤害', [], false, 'rare'),
                    new Card('绝对防御', 3, 'defend', 0, '获得15点护甲', [], false, 'rare'),
                    new Card('全面成长', 3, 'skill', 0, '获得2点所有属性', [], false, 'rare'),
                    // 新增联动卡（稀有）
                    new Card('临界爆发', 2, 'attack', 14, '造成14点法术伤害；若打出后能量=0，额外+8；若手牌≥6，改为对所有敌人造成10点', [], true, 'rare')
                ]
            }
        };
    }

    // 获取角色专属卡牌（合并所有稀有度）
    getCharacterCards(characterType) {
        const pools = this.cards.characters?.[characterType] || {};
        return [
            ...(pools.common || []),
            ...(pools.uncommon || []),
            ...(pools.rare || [])
        ];
    }

    // 获取角色按稀有度的卡牌
    getCharacterCardsByRarity(characterType, rarity = 'common') {
        return (this.cards.characters?.[characterType]?.[rarity]) || [];
    }

    // 获取通用卡牌（合并所有稀有度）
    getCommonCards() {
        const pools = this.cards.common || {};
        return [
            ...(pools.common || []),
            ...(pools.uncommon || []),
            ...(pools.rare || [])
        ];
    }

    // 获取通用卡牌（按稀有度）
    getCommonCardsByRarity(rarity = 'common') {
        return (this.cards.common?.[rarity]) || [];
    }

    // 获取所有通用卡牌（按稀有度分类）
    getAllCommonByRarity() {
        return {
            common: this.cards.common?.common || [],
            uncommon: this.cards.common?.uncommon || [],
            rare: this.cards.common?.rare || []
        };
    }
}

// 卡牌效果处理器
class CardEffectProcessor {
    static processCard(card, player, target = null) {
        const effects = [];
        
        switch (card.name) {
            case '重拳':
                effects.push({
                    type: 'damage',
                    value: card.getDamage(player),
                    target: target
                });
                break;
                
            case '重击':
                effects.push({
                    type: 'damage',
                    value: card.getDamage(player),
                    target: target
                });
                break;
                
            case '愤怒':
                effects.push({
                    type: 'addStrength',
                    value: 2,
                    target: player
                });
                break;
                
            case '坚韧':
                effects.push({
                    type: 'addBlock',
                    value: card.getBlock(player),
                    target: player
                });
                effects.push({
                    type: 'addStrength',
                    value: 1,
                    target: player
                });
                break;
                
            case '狂暴':
                effects.push({
                    type: 'addStrength',
                    value: 3,
                    target: player
                });
                effects.push({
                    type: 'damage',
                    value: 1,
                    target: player
                });
                break;
                
            case '粉碎':
                effects.push({
                    type: 'damage',
                    value: card.getDamage(player),
                    target: target
                });
                break;
                
            case '战吼':
                effects.push({
                    type: 'addStrength',
                    value: 2,
                    target: player
                });
                effects.push({
                    type: 'drawCard',
                    value: 1,
                    target: player
                });
                break;
                
            case '蛮力':
                effects.push({
                    type: 'damage',
                    value: card.getDamage(player),
                    target: target
                });
                effects.push({
                    type: 'addStrength',
                    value: 1,
                    target: player
                });
                break;
                
            case '不屈':
                effects.push({
                    type: 'addBlock',
                    value: card.getBlock(player),
                    target: player
                });
                if (player.health < player.maxHealth * 0.5) {
                    effects.push({
                        type: 'addStrength',
                        value: 2,
                        target: player
                    });
                }
                break;
                
            case '终结':
                effects.push({
                    type: 'damage',
                    value: card.getDamage(player),
                    target: target
                });
                effects.push({
                    type: 'consumeEnergy',
                    value: player.energy,
                    target: player
                });
                break;
                
            // 玲的卡牌效果
            case '快速攻击':
                effects.push({
                    type: 'damage',
                    value: card.getDamage(player),
                    target: target
                });
                break;
                
            case '闪避':
                effects.push({
                    type: 'addBlock',
                    value: card.getBlock(player),
                    target: player
                });
                effects.push({
                    type: 'drawCard',
                    value: 1,
                    target: player
                });
                break;
                
            case '连击':
                effects.push({
                    type: 'damage',
                    value: card.getDamage(player),
                    target: target
                });
                effects.push({
                    type: 'drawCard',
                    value: 1,
                    target: player
                });
                break;
                
            case '敏捷':
                effects.push({
                    type: 'addDexterity',
                    value: 2,
                    target: player
                });
                break;
                
            case '疾风':
                effects.push({
                    type: 'damage',
                    value: card.getDamage(player),
                    target: target
                });
                if (player.handSize < 3) {
                    effects.push({
                        type: 'drawCard',
                        value: 1,
                        target: player
                    });
                }
                break;
                
            case '影分身':
                effects.push({
                    type: 'addDexterity',
                    value: 3,
                    target: player
                });
                effects.push({
                    type: 'drawCard',
                    value: 2,
                    target: player
                });
                break;
                
            case '连环踢':
                effects.push({
                    type: 'damage',
                    value: card.getDamage(player),
                    target: target
                });
                effects.push({
                    type: 'drawCard',
                    value: 1,
                    target: player
                });
                break;
                
            case '风之舞':
                effects.push({
                    type: 'addBlock',
                    value: card.getBlock(player),
                    target: player
                });
                effects.push({
                    type: 'addDexterity',
                    value: 1,
                    target: player
                });
                break;
                
            case '瞬移':
                effects.push({
                    type: 'addDexterity',
                    value: 2,
                    target: player
                });
                effects.push({
                    type: 'immunity',
                    value: 1,
                    target: player
                });
                break;
                
            case '千手观音':
                effects.push({
                    type: 'damage',
                    value: card.getDamage(player),
                    target: target
                });
                effects.push({
                    type: 'drawCard',
                    value: 3,
                    target: player
                });
                break;
                
            // 澈的卡牌效果
            case '学习':
                effects.push({
                    type: 'addIntelligence',
                    value: 1,
                    target: player
                });
                effects.push({
                    type: 'drawCard',
                    value: 1,
                    target: player
                });
                break;
                
            case '成长':
                effects.push({
                    type: 'addStrength',
                    value: 1,
                    target: player
                });
                effects.push({
                    type: 'addDexterity',
                    value: 1,
                    target: player
                });
                effects.push({
                    type: 'addIntelligence',
                    value: 1,
                    target: player
                });
                break;
                
            case '基础攻击':
                effects.push({
                    type: 'damage',
                    value: card.getDamage(player),
                    target: target
                });
                break;
                
            case '基础防御':
                effects.push({
                    type: 'addBlock',
                    value: card.getBlock(player),
                    target: player
                });
                break;
                
            case '专注':
                effects.push({
                    type: 'addIntelligence',
                    value: 2,
                    target: player
                });
                effects.push({
                    type: 'drawCard',
                    value: 1,
                    target: player
                });
                break;
                
            case '模仿':
                effects.push({
                    type: 'copyCard',
                    value: 1,
                    target: player
                });
                break;
                
            case '创新':
                effects.push({
                    type: 'addIntelligence',
                    value: 2,
                    target: player
                });
                effects.push({
                    type: 'drawCard',
                    value: 2,
                    target: player
                });
                break;
                
            case '适应':
                effects.push({
                    type: 'adaptive',
                    value: 1,
                    target: player
                });
                break;
                
            case '突破':
                effects.push({
                    type: 'addIntelligence',
                    value: 3,
                    target: player
                });
                effects.push({
                    type: 'drawCard',
                    value: 3,
                    target: player
                });
                break;
                
            case '觉醒':
                effects.push({
                    type: 'addStrength',
                    value: 5,
                    target: player
                });
                effects.push({
                    type: 'addDexterity',
                    value: 5,
                    target: player
                });
                effects.push({
                    type: 'addIntelligence',
                    value: 5,
                    target: player
                });
                break;
            
            // 澈的专属法术牌
            case '法术冲击':
            case '奥术飞弹':
            case '元素爆发':
                effects.push({
                    type: 'damage',
                    value: card.getDamage(player),
                    target: target
                });
                break;
                
            case '智慧之箭':
                effects.push({
                    type: 'damage',
                    value: card.getDamage(player),
                    target: target
                });
                effects.push({
                    type: 'addIntelligence',
                    value: 1,
                    target: player
                });
                break;
                
            case '魔法护盾':
                effects.push({
                    type: 'addBlock',
                    value: card.getBlock(player),
                    target: player
                });
                effects.push({
                    type: 'addIntelligence',
                    value: 1,
                    target: player
                });
                break;
                
            case '智慧风暴':
                effects.push({
                    type: 'damage',
                    value: card.getDamage(player),
                    target: target
                });
                effects.push({
                    type: 'addIntelligence',
                    value: 2,
                    target: player
                });
                break;
                
            case '知识爆炸':
                effects.push({
                    type: 'damage',
                    value: card.getDamage(player),
                    target: target
                });
                effects.push({
                    type: 'drawCard',
                    value: 2,
                    target: player
                });
                break;
            
            // 旭的法术牌
            case '火焰打击':
                effects.push({
                    type: 'damage',
                    value: card.getDamage(player),
                    target: target
                });
                if (player.strength >= 3) {
                    effects.push({
                        type: 'damage',
                        value: 5,
                        target: target
                    });
                }
                break;
                
            case '战意燃烧':
                effects.push({
                    type: 'addStrength',
                    value: 1,
                    target: player
                });
                effects.push({
                    type: 'addIntelligence',
                    value: 1,
                    target: player
                });
                break;
                
            case '元素重击':
                effects.push({
                    type: 'damage',
                    value: card.getDamage(player),
                    target: target
                });
                effects.push({
                    type: 'addStrength',
                    value: 1,
                    target: player
                });
                break;
            
            // 玲的法术牌
            case '冰霜射击':
                effects.push({
                    type: 'damage',
                    value: card.getDamage(player),
                    target: target
                });
                effects.push({
                    type: 'drawCard',
                    value: 1,
                    target: player
                });
                break;
                
            case '风刃':
                effects.push({
                    type: 'damage',
                    value: card.getDamage(player),
                    target: target
                });
                if (player.dexterity >= 3) {
                    effects.push({
                        type: 'drawCard',
                        value: 1,
                        target: player
                    });
                }
                break;
                
            case '闪电链':
                effects.push({
                    type: 'damage',
                    value: card.getDamage(player),
                    target: target
                });
                effects.push({
                    type: 'addDexterity',
                    value: 1,
                    target: player
                });
                break;
                
            case '秘法步法':
                effects.push({
                    type: 'addDexterity',
                    value: 1,
                    target: player
                });
                effects.push({
                    type: 'addIntelligence',
                    value: 1,
                    target: player
                });
                break;
            
            // 通用法术牌
            case '魔法弹':
            case '火球术':
                effects.push({
                    type: 'damage',
                    value: card.getDamage(player),
                    target: target
                });
                break;
                
            case '奥术智慧':
                effects.push({
                    type: 'addIntelligence',
                    value: 1,
                    target: player
                });
                effects.push({
                    type: 'drawCard',
                    value: 1,
                    target: player
                });
                break;
        }
        
        return effects;
    }
}

// 全局卡牌数据库实例
const cardDatabase = new CardDatabase();
